class Createmap {
    constructor(boxname, size) {
        this.x = size[0];
        this.y = size[1];
        this.scoreNum = 0;
        this.chunkStatus = 1;
        this.box = document.querySelector(boxname);
        this.tab = document.createElement('table');
        this.scorebox = document.createElement('div');
        this.init()  
    }
    
    init() {
        this.createdata();
        this.createorigin();
        this.rendermap();
        this.rabdomChunkNum()
    }
    //创建数据
    createdata() {
        // 列
        var data = new Array(this.y);
        for (var i = 0; i < data.length; i++) {
            // 行
            var line = new Array(this.x);
            for(var j = 0; j < line.length; j++) {
                line[j] = {}
            }
            data[i] = line
        }
        this.data = data
    }
    //渲染map
    rendermap() {
        this.box.innerHTML = '';
        this.tab.innerHTML = '';
        this.scorebox.innerHTML = '';
        this.data.map((val) => {
            var tr = document.createElement('tr')
            val.map((va) => {
                var td = document.createElement('td')
                if(va.status === 1) {
                    td.className = 'active'
                }else if(va.status === 2) {
                    td.className = 'over'
                }
                tr.appendChild(td)
            })
            this.tab.appendChild(tr)
        })
        this.scorebox.innerHTML = '得分：' + this.scoreNum
        this.box.appendChild(this.scorebox)
        this.box.appendChild(this.tab)
    }
    // 生成原点
    createorigin() {
        // 解封左右移动键
        this.lefthide = true;
        this.righthide = true;
        //解封变换键
        this.changhide = true;
        //还原块状态
        this.chunkStatus = 1;
        this.origin = {};
        this.origin.oriX = Math.ceil(this.x / 2);
        this.origin.oriY = -2 ;
    }
    //随机生成块
    rabdomChunkNum() {
        //生成1-5之间的随机整数
        this.chunkMark = Math.floor(Math.random() * 5 + 1);
        this.position = this.updatePosition(this.chunkStatus);
    }
    // 一字型块
    chunkOnePosition(val) {
        // 得到组成块的方格的坐标
        if(val % 2 === 0){
            return [
                {posX: this.origin.oriX, posY: this.origin.oriY + 1},
                {posX: this.origin.oriX, posY: this.origin.oriY},
                {posX: this.origin.oriX, posY: this.origin.oriY - 1},
                {posX: this.origin.oriX, posY: this.origin.oriY - 2},
            ]
        }else{
            return [
                {posX: this.origin.oriX - 1, posY: this.origin.oriY},
                {posX: this.origin.oriX, posY: this.origin.oriY},
                {posX: this.origin.oriX + 1, posY: this.origin.oriY},
                {posX: this.origin.oriX + 2, posY: this.origin.oriY},
            ]
        }
    }
    // 土字型块
    chunkTwoPosition(val) {
        if(val === 1) {
            return [
                {posX: this.origin.oriX, posY: this.origin.oriY},
                {posX: this.origin.oriX - 1, posY: this.origin.oriY},
                {posX: this.origin.oriX + 1, posY: this.origin.oriY},
                {posX: this.origin.oriX, posY: this.origin.oriY - 1},
            ]
        }else if(val === 2) {
            return [
                {posX: this.origin.oriX, posY: this.origin.oriY},
                {posX: this.origin.oriX, posY: this.origin.oriY + 1},
                {posX: this.origin.oriX + 1, posY: this.origin.oriY},
                {posX: this.origin.oriX, posY: this.origin.oriY - 1},
            ]
        }else if(val === 3) {
            return [
                {posX: this.origin.oriX, posY: this.origin.oriY},
                {posX: this.origin.oriX - 1, posY: this.origin.oriY},
                {posX: this.origin.oriX + 1, posY: this.origin.oriY},
                {posX: this.origin.oriX, posY: this.origin.oriY + 1},
            ]
        }else if(val === 4) {
            return [
                {posX: this.origin.oriX, posY: this.origin.oriY},
                {posX: this.origin.oriX - 1, posY: this.origin.oriY},
                {posX: this.origin.oriX, posY: this.origin.oriY + 1},
                {posX: this.origin.oriX, posY: this.origin.oriY - 1},
            ]
        }
    }
    // 方型块
    chunkThreePosition(){
        return [
            {posX: this.origin.oriX, posY: this.origin.oriY},
            {posX: this.origin.oriX + 1, posY: this.origin.oriY},
            {posX: this.origin.oriX, posY: this.origin.oriY - 1},
            {posX: this.origin.oriX + 1, posY: this.origin.oriY - 1},
        ]
    }
    // 7字型块
    chunkFourPosition(val) {
        if(val === 1) {
            return [
                {posX: this.origin.oriX, posY: this.origin.oriY + 1},
                {posX: this.origin.oriX, posY: this.origin.oriY},
                {posX: this.origin.oriX, posY: this.origin.oriY - 1},
                {posX: this.origin.oriX - 1, posY: this.origin.oriY - 1},
            ]
        }else if(val === 2) {
            return [
                {posX: this.origin.oriX - 1, posY: this.origin.oriY},
                {posX: this.origin.oriX, posY: this.origin.oriY},
                {posX: this.origin.oriX + 1, posY: this.origin.oriY},
                {posX: this.origin.oriX + 1, posY: this.origin.oriY - 1},
            ]
        }else if(val === 3) {
            return [
                {posX: this.origin.oriX, posY: this.origin.oriY - 1},
                {posX: this.origin.oriX, posY: this.origin.oriY},
                {posX: this.origin.oriX, posY: this.origin.oriY + 1},
                {posX: this.origin.oriX + 1, posY: this.origin.oriY +1},
            ]
        }else if(val === 4) {
            return [
                {posX: this.origin.oriX + 1, posY: this.origin.oriY},
                {posX: this.origin.oriX, posY: this.origin.oriY},
                {posX: this.origin.oriX - 1, posY: this.origin.oriY},
                {posX: this.origin.oriX - 1, posY: this.origin.oriY + 1},
            ]
        }
    }
    // 闪电型块
    chunkFivePosition(val) {
        if(val % 2 === 0){
            return [
                {posX: this.origin.oriX, posY: this.origin.oriY + 1},
                {posX: this.origin.oriX, posY: this.origin.oriY},
                {posX: this.origin.oriX - 1, posY: this.origin.oriY},
                {posX: this.origin.oriX - 1, posY: this.origin.oriY - 1},
            ]
        }else{
            return [
                {posX: this.origin.oriX - 1, posY: this.origin.oriY},
                {posX: this.origin.oriX, posY: this.origin.oriY},
                {posX: this.origin.oriX, posY: this.origin.oriY - 1},
                {posX: this.origin.oriX + 1, posY: this.origin.oriY - 1},
            ]
        }
    }
    //更新位置 
    updatePosition(val) {
        var pos
        switch (this.chunkMark){
            case 1:
                pos = this.chunkOnePosition(val)
                break
            case 2:
                pos = this.chunkTwoPosition(val)
                break
            case 3:
                pos = this.chunkThreePosition(val)
                break
            case 4:
                pos = this.chunkFourPosition(val)
                break
            case 5:
                pos = this.chunkFivePosition(val)
        }
        return pos
    }
    // 左右键状态
    leftRightStatus() {
        // 禁止左右移栋
        for (var item of this.position) {
            if(item.posX === 0 || item.posY >=0 && this.data[item.posY][item.posX -1].status === 2) {
                this.lefthide = false;
                break
            }else {
                this.lefthide = true;
            }
            if(item.posX === this.x - 1 || item.posY >=0 && this.data[item.posY][item.posX+1].status === 2){
                this.righthide = false;
                break
            }else{
                this.righthide = true;
            }
        }
    }
    // 变换键状态
    changeStatus() {
        var chunkStatusA = this.chunkStatus;
        if(chunkStatusA === 4){
            chunkStatusA = 1
        }else{
            chunkStatusA++;
        }
        var positionA = this.updatePosition(chunkStatusA);
        for (var item of positionA) {
            if(item.posX < 0 || item.posX > this.x - 1 || item.posY > this.y-1 || item.posY >= 0 && this.data[item.posY][item.posX].status === 2 ) {
                this.changhide = false;
                break
            }else {
                this.changhide = true;
            }
        }
    }
    // 创建块
    createChunk() {
        this.leftRightStatus();
        this.changeStatus();
        //判断本轮是否结束，开启下一轮
        for (var item of this.position) {
            if(item.posY === this.y-1){
                this.position.map((val) => {
                    this.data[val.posY][val.posX].status = 2;
                })
                this.score();
                this.createorigin();
                this.rabdomChunkNum();
                break
            }else if( item.posY >= -1 && this.data[item.posY+1][item.posX].status === 2){
                this.position.map((val) => {
                    this.data[val.posY][val.posX].status = 2;
                })
                this.score();
                this.createorigin();
                this.rabdomChunkNum();
                break
            }
        }
        this.position.map((val) => {
            if(val.posY >= 0 && val.posY < this.y - 1) {
                this.data[val.posY][val.posX].status = 1;
            } 
        })
    }
    // 清除上一次块
    clearchunk() {
        this.position.map((item) => {
            if(item.posY >= 0 && item.posX >= 0 && item.posX <= this.x && item.posY <= this.y){
                this.data[item.posY][item.posX].status = 0;
            }
        })
        
    }
    // 消除得分
    score() {
        // var staNum = 0
        this.data.forEach((item, index, arr) => {
            // 算法太慢影响下一次块是准备
            // item.map((val) => {
            //     if(val.status === 2) {
            //         staNum +=1
            //     }
            // })
            // if(staNum === this.x){
            //     this.scoreNum += 10;
            //     arr.splice(index, 1);
            //     arr.unshift(x)
                
            // }
            // staNum = 0
            
            //判断每行是否存在status不等于2，也就是一行没有占满
            var  statusTwo = item.some((val) => val.status != 2 )
            if(!statusTwo ){
                this.scoreNum += 10;
                // 删除status全部为2的那行
                arr.splice(index, 1);
                var line = new Array(this.x);
                for(var j = 0; j < line.length; j++) {
                    line[j] = {}
                }
                // 在表格最前面添加一行
                arr.unshift(line)
            }
        })
        
    }
    // 加速
    moveBottom() {
        this.clearchunk()
        this.origin.oriY +=1;
        this.position = this.updatePosition(this.chunkStatus);
        this.createChunk();
        this.rendermap()
    }
    // 右移
    moveRight() {
        if(!this.lefthide){
            this.lefthide = true
        }
        if(this.righthide){
            this.clearchunk()
            this.origin.oriX +=1;
            this.position = this.updatePosition(this.chunkStatus);
            this.createChunk();
            this.rendermap()
        } 
    }
    moveLeft() {
        if(!this.righthide){
            this.righthide = true
        }
        if(this.lefthide){
            this.clearchunk()
            this.origin.oriX -=1;
            this.position = this.updatePosition(this.chunkStatus);
            this.createChunk();
            this.rendermap()
        } 
    }
    changeChunk() {
        if(this.chunkMark === 3 || !this.changhide) {return};
        if(this.chunkStatus === 4){this.chunkStatus = 0}
        this.clearchunk();
        this.chunkStatus ++;
        this.position = this.updatePosition(this.chunkStatus);
        this.createChunk();
        this.rendermap();
        
    }
} 
// 注意：引用类型